//
//  RCEmptyViewOC.m
//  RedianClient
//
//  Created by ayi on 2020/7/23.
//  Copyright © 2020 bsln. All rights reserved.
//

#import "RCEmptyViewOC.h"

@interface RCEmptyViewOC()

//store
@property (nonatomic, copy) NSString *cacheTitleStr;
@property (nonatomic, copy) NSString *cacheDetailStr;
@property (nonatomic, copy) NSString *cacheBtnTitleStr;
@property (nonatomic, strong) UIImage *cacheImg;
@property (nonatomic, copy) dispatch_block_t cacheblock;
@property (nonatomic, copy) RCEmptyViewOCErrorBlock errorblock;

@end

@implementation RCEmptyViewOC


- (void)prepare{
    [super prepare];
    self.contentViewOffset = 0;
//    self.contentViewY = 100;
    
//    self.titleLabFont = FONT_SIZE(14);
//    self.titleLabTextColor = RGBS(@"#333333");
//
//    self.detailLabFont = FONT_SIZE(14);
//    self.detailLabTextColor = RGBS(@"#333333");
//    self.detailLabMaxLines = 0;
//
//    self.actionBtnFont = FONT_SIZE(12);
//    self.actionBtnTitleColor = UIColor.whiteColor;
//    self.actionBtnHeight = 30;
//    self.actionBtnBackGroundColor = RGBS(@"#000000");
    
    self.detailLabMargin = 10;
//    self.actionBtnMargin = 15;
}


+ (UIImage *)returnTypeImg:(RCEmpty_Type )type{
    UIImage *emptyimg;
    switch (type) {
        case RCEmpty_ErrorType_Error:
//            emptyimg = IMG(@"empty_img_network");
            break;
        case RCEmpty_ErrorType_NetWork:
//            emptyimg = IMG(@"empty_img_network");
            break;
        case RCEmpty_ErrorType_404:
//            emptyimg = IMG(@"empty_img_404");
            break;
        case RCEmpty_ErrorType_Login:
//            emptyimg = IMG(@"empty_img_login");
            break;
        default:
//            emptyimg = IMG(@"empty_img_network");
            break;
    }
    return emptyimg;
}

- (void)rc_startLoading{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.superview ly_startLoading];
    });
}

- (void)rc_endLoading{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.superview ly_endLoading];
    });
}

- (void)rc_showEmptyview{
    [self.superview ly_showEmptyView];
}
- (void)rc_hideEmptyview{
    [self.superview ly_hideEmptyView];
}

- (void)setCover_Superview:(BOOL)cover_Superview{
    self.emptyViewIsCompleteCoverSuperView = cover_Superview;
}

- (void)setRc_fixContentInsetViewX:(BOOL)rc_fixContentInsetViewX{
    self.fixContentInsetViewX = rc_fixContentInsetViewX;
}


+ (RCEmptyViewOC *)dataEmpty{
    return [RCEmptyViewOC dataEmpty:RCEmpty_ErrorType_Error];
}

+ (RCEmptyViewOC *)dataEmpty:(RCEmpty_Type )type{
    NSString *emptyStr = @"暂无数据";
    RCEmptyViewOC *empty = [RCEmptyViewOC dataEmpty:type andEmptyStr:emptyStr];
    return empty;
}

+ (RCEmptyViewOC *)dataEmpty:(RCEmpty_Type )type andEmptyStr:(NSString *)emptystr{
    UIImage *emptyimg = [RCEmptyViewOC returnTypeImg:type];
    NSString *emptyStr = emptystr;
    
    RCEmptyViewOC *empty = [RCEmptyViewOC emptyViewWithImage:emptyimg titleStr:emptystr detailStr:nil];
    
    empty.cacheImg = emptyimg;
    empty.cacheTitleStr = emptyStr;
    
    return empty;
}


+ (RCEmptyViewOC *)dataEmpty:(RCEmpty_Type )type
                    andTitle:(NSString *)title
                   andDetail:(NSString *)detail
                 andBtnTitle:(NSString *)btnTitle
                 andBtnBlock:(dispatch_block_t)clickAction
               andErrorBlock:(RCEmptyViewOCErrorBlock )errorAction{
    
    RCEmptyViewOC *empty = [RCEmptyViewOC dataEmptyCustomImg:[RCEmptyViewOC returnTypeImg:type] andTitle:title andDetail:detail andBtnTitle:btnTitle andBtnBlock:clickAction andErrorBlock:errorAction];
    return empty;
}

+ (RCEmptyViewOC *)dataEmptyCustomImg:(UIImage *)img
                             andTitle:(NSString *)title
                            andDetail:(NSString *)detail
                          andBtnTitle:(NSString *)btnTitle
                          andBtnBlock:(dispatch_block_t)clickAction
                        andErrorBlock:(RCEmptyViewOCErrorBlock )errorAction{
    
    LYActionTapBlock clickBlock;
    if (clickAction) {
        clickBlock = ^(void){
            clickAction ? clickAction() : nil;
        };
    }
    RCEmptyViewOC *empty = [RCEmptyViewOC emptyActionViewWithImage:img titleStr:title detailStr:detail btnTitleStr:btnTitle btnClickBlock:clickBlock];
    
    
    empty.cacheImg = img;
    empty.cacheTitleStr = title;
    empty.cacheDetailStr = detail;
    empty.cacheblock = clickAction;
    empty.errorblock = errorAction;
    
    return empty;
}



+ (RCEmptyViewOC *)emptyCustomView:(UIView *)view{
    return [RCEmptyViewOC emptyViewWithCustomView:view];
}

- (void)setError:(NSError *)error{
    [self rc_endLoading];
    
    _error = error;
    
    if (error != nil) {
        
        if (error.code == 0) {
            self.titleStr = [NSString stringWithFormat:@"%@",error.domain];
        }else{
            self.titleStr = @" ";
        }
//        if ([Bx_AfnNetManager shareInstance].networkStats == StatusUnknown) {
//            self.detailStr = @"似乎断开了互联网连接";
//        }else{
//            self.detailStr = error.localizedDescription;
//        }
        self.detailStr = error.localizedDescription;
        
    }else{
        self.titleStr = self.cacheTitleStr;
        self.detailStr = self.cacheDetailStr;
        self.btnTitleStr = self.cacheBtnTitleStr;
        self.image = self.cacheImg;
        if (self.cacheblock ){
            @empty_weakify(self);
            self.btnClickBlock = ^(void){
                @empty_strongify(self);
                self.cacheblock ? self.cacheblock() : nil;
            };
        }else{
            self.btnClickBlock = nil;
        }
    }
}

- (void)dataEmptyWithError:(NSError *)error{
    [self dataEmptyWithError:error andImg:nil andBtnTitle:nil andBtnAction:nil];
}

- (void)dataEmptyErrorType:(RCEmpty_Type )type WithError:(NSError *)error andBtnTitle:(NSString *)btnTitle{
    [self dataEmptyErrorType:type WithError:error andBtnTitle:btnTitle andBtnAction:nil];
}

- (void)dataEmptyErrorType:(RCEmpty_Type )type WithError:(NSError *)error andBtnTitle:(NSString *)btnTitle andBtnAction:(dispatch_block_t )action{
    [self dataEmptyWithError:error andImg:[RCEmptyViewOC returnTypeImg:type] andBtnTitle:btnTitle andBtnAction:action];
}

- (void)dataEmptyWithError:(NSError *)error andImg:(UIImage *)errorImg andBtnTitle:(NSString *)btnTitle{
    [self dataEmptyWithError:error andImg:errorImg andBtnTitle:btnTitle andBtnAction:nil];
}

- (void)dataEmptyWithError:(NSError *)error andImg:(UIImage *)errorImg andBtnTitle:(NSString *)btnTitle andBtnAction:(dispatch_block_t )action{
    
    if (error != nil) {
        if (errorImg != nil) {
            self.image = errorImg;
        }
        if (btnTitle != nil) {
            self.btnTitleStr = btnTitle;
        }
        @empty_weakify(self);
        if (action != nil ){
            self.btnClickBlock = ^(void){
                action ? action() : nil;
            };
        }else if(self.errorblock != nil){
            self.btnClickBlock = ^(void){
                @empty_strongify(self);
                self.errorblock ? self.errorblock(error) : nil;
            };
        }else{
            self.btnClickBlock = nil;
        }
    }
    
    self.error = error;
}


@end
